package com.example.springbootmybatis.controller;

import com.example.springbootmybatis.SpringbootMybatisApplication;
import com.example.springbootmybatis.entity.CodeValue;
import com.example.springbootmybatis.entity.Product;
import com.example.springbootmybatis.entity.RestResult;
import com.example.springbootmybatis.service.CodeValueService;
import com.example.springbootmybatis.service.ProductService;
import com.example.springbootmybatis.utility.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @title: ProductController
 * @Author Tan
 * @Date: 2021/6/24 14:45
 * @Version 1.0
 */
@Controller
@RequestMapping(value = "/product")
public class ProductController {
    @Resource
    private ProductService productService;
    @Resource
    private CodeValueService codeValueService;

    public static Logger logger = LoggerFactory.getLogger(SpringbootMybatisApplication.class);

    @RequestMapping(value = "/search", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    @ResponseBody
    public RestResult<Map<String, Object>> Search(@RequestParam Map<String,Object> param) {
        System.out.println(param.toString());
        Map<String, Object> map = new HashMap<String, Object>();
        List<Product> productList = productService.selectHomePageDataList();
        map.put("productList", productList);
        List<CodeValue> productCatgoryList = codeValueService.SearchProductCatgory();
        map.put("productCatgoryList", productCatgoryList);
        return ResultUtil.genSuccessResult(map);
    }

    @RequestMapping(value = "/updateProduct", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    @ResponseBody
    public RestResult<Map<String, Object>> updateProduct(@RequestParam Map<String, Object> param) {
        logger.debug(param.toString());
        Map<String, Object> map = new HashMap<String, Object>();
        int count = productService.updateProduct((List<Product>) param.get("productList"));
        map.put("count", count);
        return ResultUtil.genSuccessResult(map);
    }
}
